package pers.cnzdy.tutorial.Chapter5_UI.Section7_ListView;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import pers.cnzdy.tutorial.R;


public class KnowledgePointsAdapter extends
        RecyclerView.Adapter<KnowledgePointsAdapter.ViewHolder> {


    static class ViewHolder extends RecyclerView.ViewHolder {
        View      kpointView;
        ImageView knowledgePointIcon;
        TextView  knowledgePointTitle;

        public ViewHolder(View view) {
            super(view);
            kpointView = view;
            knowledgePointIcon = view.findViewById(R.id.knowledge_point_icon);
            knowledgePointTitle = view.findViewById(R.id.knowledge_point_title);
        }
    }

    private List<KnowledgePoints.KnowledgePoint> knowledgePointList;

    public KnowledgePointsAdapter(
            List<KnowledgePoints.KnowledgePoint> knowledgePointList) {
//        if (BuildConfig.DEBUG && knowledgePointList == null) {
//            Log.d(TAG, "KnowledgePointsAdapter: knowledgePointList == null");
//        }

        this.knowledgePointList = knowledgePointList;
    }

    @Override
    public int getItemCount() {
        return knowledgePointList.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.knowledge_point_item, parent, false);

        final ViewHolder holder = new ViewHolder(view);

        //-----------------------------------------------//
        // 点击事件
        holder.kpointView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                KnowledgePoints.KnowledgePoint kpoint = knowledgePointList.get(position);
                Toast.makeText(v.getContext(), "知识点：" +
                        kpoint.getCaption(), Toast.LENGTH_SHORT).show();
            }
        });

        holder.knowledgePointIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                KnowledgePoints.KnowledgePoint kpoint = knowledgePointList.get(position);
                Toast.makeText(v.getContext(), "按在图片上，知识点：" +
                        kpoint.getCaption(), Toast.LENGTH_SHORT).show();
            }
        });
        //-----------------------------------------------//

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        KnowledgePoints.KnowledgePoint kpoint = knowledgePointList.get(position);
        holder.knowledgePointIcon.setImageResource(kpoint.getImageID());
        holder.knowledgePointTitle.setText(kpoint.getCaption());
    }
}